package controler;

import java.awt.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;


public class LoadSaveManager {

	public static final String PROPERTY_FILE_NAME = "taskManager.properties";
	public static final String CURRENT_VERSION = "V0_1";
	private static final File TEMP_DIR = new File(System.getProperty("java.io.tmpdir"));
	private static final String DEFAULT_DATA_PATH = "C:\\Developpement\\Java\\data\\TaskManagerWS";
	
	private EnumMap<PropertyE, String> propertyMap = new EnumMap<PropertyE, String>(PropertyE.class);
	private File currentWorkspace = null;
	
	public LoadSaveManager(Controler controler) {
		File propertyFile = new File(TEMP_DIR, PROPERTY_FILE_NAME);
		if (!propertyFile.exists()) {
			selectFirstWorkspace(controler);
		} else {
			loadProperties(controler);
		}
	}
	
	private void selectFirstWorkspace(Controler controler) {
		setProperty(PropertyE.CURRENT_TASKMANAGER_VERSION, CURRENT_VERSION, false);
		setProperty(PropertyE.WORKSPACE, DEFAULT_DATA_PATH, false);
		currentWorkspace = new File(DEFAULT_DATA_PATH);
	}
	
	public void setProperty(PropertyE property, String value) {
		setProperty(property, value, true);
	}
	
	public void setProperty(PropertyE property, String value, boolean save) {
		propertyMap.put(property, value);
		if(save) {
			saveProperties();
		}
	}
	
	public String getProperty(PropertyE property) {
		return propertyMap.get(property);
	}
	
	public void saveProperties() {
		File propertyFile = new File(TEMP_DIR, PROPERTY_FILE_NAME);
		try {
			PrintStream ps = new PrintStream(propertyFile);
			for (PropertyE prop : propertyMap.keySet()) {
				ps.println(prop +  " = " + propertyMap.get(prop));
			}
			ps.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void loadProperties(Controler controler) {
		File propertyFile = new File(TEMP_DIR, PROPERTY_FILE_NAME);
		List<String> lines = readLines(propertyFile);
		for (String line : lines) {
			String[] ch = line.split(" = ");
			if (ch.length == 2) {
				String key = ch[0].trim();
				if (key.startsWith(PropertyE.WORKSPACE.toString())) {
					currentWorkspace = new File(ch[1].trim());
					propertyMap.put(PropertyE.WORKSPACE, ch[1].trim());
				} else {
					PropertyE prop = PropertyE.valueOf(key.trim());
					String value = ch[1].trim();
					propertyMap.put(prop, value);
				}
			}
		}
		
		setProperty(PropertyE.CURRENT_TASKMANAGER_VERSION, CURRENT_VERSION, false);
		
	}
	

	
	public File getWorkspace() {
		return currentWorkspace;
	}
	
	public static List<String> readLines(File f) {
		List<String> result = new ArrayList<String>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(f));
			while (reader.ready()) {
				String line = reader.readLine();
				result.add(line);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public static List<String> readLines(URL url) {
		List<String> result = new ArrayList<String>();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			while (reader.ready()) {
				String line = reader.readLine();
				result.add(line);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public void displayHardCodedTextMessage(String textName, Component parent) {
		List<String> lines = readLines(ClassLoader.getSystemClassLoader().getResource("data/messages/" + textName + ".txt"));
		String text = "";
		for (String line : lines) {
			text += line + "\n";
		}
		JTextArea area = new JTextArea(text);
		area.setEditable(false);
		
		JOptionPane.showMessageDialog(parent, area);
	}

}
